package com.itheima.ydd.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.ydd.common.ResultData;
import com.itheima.ydd.entity.YddUser;
import com.itheima.ydd.service.IYddUserService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2022-05-22
 */

//会员管理
@RestController
@RequestMapping("/ydd_user")
public class YddUserController {

    @Autowired
    private IYddUserService yddUserService;
    //查询所有
    @GetMapping("list")
    public ResultData list(Integer sex, String username, String mobile, Integer page, Integer limit){
        LambdaQueryWrapper<YddUser> lqw=new LambdaQueryWrapper<>();
        //根据性别等于判断,根据姓名和手机号做模糊查询r 
        lqw.eq(sex!=null,YddUser::getSex,sex).like(!StringUtils.isEmpty(username),YddUser::getUsername,username).like(!StringUtils.isEmpty(mobile),YddUser::getMobile,mobile);
        //判断账户是否被删除
        lqw.eq(YddUser::getDefaultData,1);
        Page<YddUser> yddUserPage=new Page<>(page,limit);
        //执行分页查询
        yddUserService.page(yddUserPage,lqw);

        HashMap<String, Object> map = new HashMap<>();
        map.put("data", yddUserPage.getRecords());
        map.put("count",Math.toIntExact(yddUserPage.getTotal()));
        map.put("totalPages",Math.toIntExact(yddUserPage.getSize()));
        map.put("pageSize", limit);
        map.put("currentPage",Math.toIntExact(yddUserPage.getCurrent()));
        return ResultData.ok("查询成功",map);
    }


    @PostMapping("add")
    public ResultData add(@RequestBody YddUser user){
        //先判断传回来的手机号码是否存在
        LambdaQueryWrapper<YddUser> lqw=new LambdaQueryWrapper<>();
        lqw.eq(YddUser::getMobile,user.getMobile());
        YddUser yddUser = yddUserService.getOne(lqw);
        //先判断是否为空,空则存入数据库
        if (null==yddUser){
            yddUserService.save(user);
            return ResultData.ok("添加成功",null);
        }
        //否则就是已被注册,直接返回错误信息

        return ResultData.error("该手机号已被注册");
    }

    //数据回显
    @GetMapping("rowInfo")
    public ResultData rowInfo(Integer id){
        //根据前端传回的id查询
        YddUser yddUser = yddUserService.getById(id);
        return ResultData.ok("0",yddUser);
    }
    //删除
    @GetMapping("del")
    public ResultData del(Integer id){
        LambdaUpdateWrapper<YddUser> lqw = new LambdaUpdateWrapper<>();
        //通过id查询,再将状态修改成已删除状态
        lqw.eq(YddUser::getId,id).set(YddUser::getDefaultData,0);
        //修改状态
        yddUserService.update(lqw);
        //将结果返回给前端
        return ResultData.ok("删除成功",null);
    }

    //修改
    @PostMapping("update")
    public ResultData update(@RequestBody YddUser yddUser){
        //先根据用户id查询,手机号是否变更
        YddUser serviceById = yddUserService.getById(yddUser.getId());
        //先判断修改后的手机号是否存在
        LambdaQueryWrapper<YddUser> lqw = new LambdaQueryWrapper<>();
        lqw.eq(YddUser::getMobile,yddUser.getMobile());
        YddUser userServiceOne = yddUserService.getOne(lqw);
        if (userServiceOne!=null&&!yddUser.getMobile().equals(serviceById.getMobile())){
            //手机号已存在,直接返回错误信息
            return ResultData.error("手机号已被注册");
        }
        //将数据根据id修改
        yddUserService.updateById(yddUser);
        return ResultData.ok("修改成功",null);
    }
}
